//-----------------------------------------------------------------------------
//	File: eeprom.c
//	Author: xialing
//		bsp of eeprom using i2c for 24c512
//-----------------------------------------------------------------------------

#include "eeprom.h"

#define WR_ADDR		0xA0
#define RD_ADDR		0xA1

//-----------------------------------------------------------------------------
//	eeprom_write - write data to eeprom
//		@addr: eeprom address
//		@dat: data to be written
//-----------------------------------------------------------------------------
void eeprom_write(uint16_t addr, uint8_t dat)
{
	do {
		i2c_eeprom_start();
		i2c_eeprom_write(WR_ADDR);
	} while (i2c_eeprom_ack());
	i2c_eeprom_write(addr >> 8);
	while (i2c_eeprom_ack());
	i2c_eeprom_write(addr & 0xFF);
	while (i2c_eeprom_ack());
	i2c_eeprom_write(dat);
	while (i2c_eeprom_ack());
	i2c_eeprom_stop();
}

//-----------------------------------------------------------------------------
//	eeprom_read - read data from eeprom
//		@addr: eeprom address
//		@pdat: data to be read
//-----------------------------------------------------------------------------
void eeprom_read(uint16_t addr, uint8_t *pdat)
{
	do {
		i2c_eeprom_start();
		i2c_eeprom_write(WR_ADDR);
	} while (i2c_eeprom_ack());
	i2c_eeprom_write(addr >> 8);
	while (i2c_eeprom_ack());
	i2c_eeprom_write(addr & 0xFF);
	while (i2c_eeprom_ack());
	i2c_eeprom_stop();

	do {
		i2c_eeprom_start();
		i2c_eeprom_write(RD_ADDR);
	} while (i2c_eeprom_ack());
	i2c_eeprom_read(pdat, 1);
	i2c_eeprom_stop();
}

